package com.cskaoyan.service.vx.topic;

import com.cskaoyan.bean.po.wxPo.MarketGoods;
import com.cskaoyan.bean.po.wxPo.MarketTopic;
import com.cskaoyan.bean.po.wxPo.MarketTopicExample;
import com.cskaoyan.bean.vo.vxVo.WxIndexTopicVO;
import com.cskaoyan.bean.vo.vxVo.topic.*;
import com.cskaoyan.mapper.wxMapper.MarketGoodsMapper;
import com.cskaoyan.mapper.wxMapper.MarketTopicMapper;
import com.cskaoyan.util.StringToArrayUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/8 11:36
 */
@Service
public class WXTopicServiceImpl implements WXTopicService{
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    /**
     * 该方法实现
     * @param id
     * @return
     */
    @Override
    public WXTopicDetailVo detail(Integer id) {
        // 分为两步:
        // 1. 从 sql:market_topic 表根据 id 查询;
        // MarketTopicExample marketTopicExample = new MarketTopicExample();
        // MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        // criteria.andIdEqualTo(id);
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id); // Po
        String goods = marketTopic.getGoods();
        List<Integer> integers = StringToArrayUtils.toArray(goods);
        // 2. 根据 1 查询出来的 goodsId 数组 ,到 sql: market_goods里去查询;
        List<MarketGoods> marketGoodsPos = new ArrayList<MarketGoods>();
        for (Integer integer : integers) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(integer);
            marketGoodsPos.add(marketGoods);
        }
        // 将 1 与 2 获取到的 PO 对象向 Vo 映射;

        VXTopicDetailInnerTopicVo vxTopicDetailInnerTopicVo = new VXTopicDetailInnerTopicVo();
        List<VXTopicDetailInnerGoodsVo> vxTopicDetailInnerGoodsVos = new ArrayList<>();
        vxTopicDetailInnerTopicVo.setId(marketTopic.getId());
        vxTopicDetailInnerTopicVo.setTitle(marketTopic.getTitle());
        vxTopicDetailInnerTopicVo.setSubtitle(marketTopic.getSubtitle());
        vxTopicDetailInnerTopicVo.setPrice(marketTopic.getPrice().doubleValue());
        vxTopicDetailInnerTopicVo.setReadCount(marketTopic.getReadCount());
        vxTopicDetailInnerTopicVo.setPicUrl(marketTopic.getPicUrl());
        vxTopicDetailInnerTopicVo.setSortOrder(marketTopic.getSortOrder());
        vxTopicDetailInnerTopicVo.setGoods(StringToArrayUtils.toArray(marketTopic.getGoods()));
        vxTopicDetailInnerTopicVo.setAddTime(marketTopic.getAddTime());
        vxTopicDetailInnerTopicVo.setUpdateTime(marketTopic.getUpdateTime());
        vxTopicDetailInnerTopicVo.setDeleted(marketTopic.getDeleted());
        vxTopicDetailInnerTopicVo.setContent(marketTopic.getContent());

        // 完成 PoList 向 VoList 的转化;
        for (int i = 0; i < vxTopicDetailInnerGoodsVos.size(); i++) {
            VXTopicDetailInnerGoodsVo temp = vxTopicDetailInnerGoodsVos.get(i);
            MarketGoods marketGoods = marketGoodsPos.get(i);
            temp.setId(marketGoods.getId());
            temp.setName(marketGoods.getName());
            temp.setBrief(marketGoods.getBrief());
            temp.setPicUrl(marketGoods.getPicUrl());
            temp.setIsNew(marketGoods.getIsNew());
            temp.setIsHot(marketGoods.getIsNew());
            temp.setCounterPrice(marketGoods.getCounterPrice().doubleValue());
            temp.setRetailPrice(marketGoods.getRetailPrice().doubleValue());
        }
        WXTopicDetailVo wxTopicDetailVo = new WXTopicDetailVo();
        wxTopicDetailVo.setTopic(vxTopicDetailInnerTopicVo);
        wxTopicDetailVo.setGoods(vxTopicDetailInnerGoodsVos);
        return wxTopicDetailVo;
    }

    @Override
    public TVO<WXTopicRelatedVo> related(Integer id) {
        // 1. 开启分页
        // 自定义显示 current page 和 limit
        int page = 1;
        int limit =4;
        PageHelper.startPage(page,limit);
        // 2. 调用 mapper ; marketTopic ;   外界传入的 id 根本不是 market_topic 表的 id;
        // MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(null);// 传入 null, 查询全部;| 与之比较: 插入一个无条件的 example 实例;

        // new 一个 voList 用来存储 ;
        ArrayList<WXTopicRelatedVo> wxTopicRelatedVos = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopics) {
            WXTopicRelatedVo wxTopicRelatedVo = new WXTopicRelatedVo();
            wxTopicRelatedVo.setId(marketTopic.getId());
            wxTopicRelatedVo.setTitle(marketTopic.getTitle());
            wxTopicRelatedVo.setSubtitle(marketTopic.getSubtitle());
            wxTopicRelatedVo.setPrice(marketTopic.getPrice().intValue()); // BigDecimal-->Integer
            wxTopicRelatedVo.setReadCount(marketTopic.getReadCount());
            wxTopicRelatedVo.setPicUrl(marketTopic.getPicUrl());
            wxTopicRelatedVo.setSortOrder(marketTopic.getSortOrder());
            wxTopicRelatedVo.setGoods(StringToArrayUtils.toArray(marketTopic.getGoods())); // String --> List;
            wxTopicRelatedVo.setAddTime(marketTopic.getAddTime());
            wxTopicRelatedVo.setUpdateTime(marketTopic.getUpdateTime());
            wxTopicRelatedVo.setDeleted(marketTopic.getDeleted());
            wxTopicRelatedVo.setContent(marketTopic.getContent());
            wxTopicRelatedVos.add(wxTopicRelatedVo);
        }
        PageInfo pageInfo = new PageInfo(wxTopicRelatedVos);

        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();



        // TVO<WXTopicRelatedVo> wxTopicRelatedVoTVO = new TVO<>();
        // wxTopicRelatedVoTVO.setLimit();
        return  TVO.data(total,pages,limit,page,wxTopicRelatedVos);
    }
}
